<div class="layui-card">

    <div class="layui-card-body">
        <div class="layui-form toolbar">
            <div class="layui-inline layui-show-xs-block">
                <input class="layui-input" autocomplete="off" placeholder="活动日期" name="start" id="start"></div>
            <button id="menus-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="business-table" lay-filter="menus-table"></table>
    </div>
</div>
<!--状态操作-->
<script type="text/html" id="switchTpl">
    <!-- 这里的checked的状态只是演示 -->
    <input type="checkbox" name="enable" value="{{d.actiid}}" lay-skin="switch" lay-text="结束活动|通过活动" lay-filter="off"
           {{d.enable=='1'?'checked':''}}>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin', 'formSelects', 'treetable', 'layer', 'laydate'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var admin = layui.admin;
        var treetable = layui.treetable;
        var config = layui.config;
        var form = layui.form;
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });
        // 渲染表格
        table.render({
            elem: '#business-table',
            url: config.base_server + 'api-administrators/businessActivity/findAll',
            where: {
                access_token: config.getToken().access_token,
            },
            page: true,
            parseData: function (res) {
                console.log(res)
                return {
                    "code": 0,
                    "msg": "",
                    "total": res.data.total,
                    "data": res.data.activity,
                    "count": res.data.count,
                };
            },
            cols: [[
                {type: 'numbers'},
                {field: 'reduce', minWidth: 200, title: '减免金额'},
                {field: 'remark', title: '活动详情'},
                {field: 'crtime', title: '活动创建'},
                {field: 'enable', width: 95, templet: '#switchTpl', title: '活动状态'}
            ]]
        });
        //监听状态操作
        form.on('switch(off)', function (obj) {
            console.log(obj.othis[0].innerText);
            if (obj.othis[0].innerText == '结束活动') {
                $.ajax({
                    url: config.base_server + 'api-administrators/businessActivity/update',//地址：访问后台的地址
                    data: {actiid: obj.value,enable: 0, access_token: config.getToken().access_token,},//前端传递给后台的数据
                    type: 'post',
                    dataType: 'json',//是后端传递到前端的数据格式
                    success: function (data) {//后端给前端的一个反馈
                        layer.msg('通过成功！')
                    }
                });
            }else {
                $.ajax({
                    url: config.base_server + 'api-administrators/businessActivity/update',//地址：访问后台的地址
                    data: {actiid: obj.value,enable: 1, access_token: config.getToken().access_token,},//前端传递给后台的数据
                    type: 'post',
                    dataType: 'json',//是后端传递到前端的数据格式
                    success: function (data) {//后端给前端的一个反馈
                        layer.msg('结束成功！')
                    }
                });
            }
        });

        // 搜索按钮点击事件
        $('#menus-btn-search').click(function () {
            var keyword = $('#start').val();
            table.reload('business-table', {where: {crtime: keyword}});
        });
    });
</script>